/*	
	Universidad: Universidad Nacional de Río Cuarto (UNRC)
	Materia: Taller de Diseño de Software (Cod. 3306)
	Carrera: Licenciatura en Ciencias de la Computación
	Proyecto 2011: Compilador de un subconjunto del lenguaje C (C--).
	Integrantes:
		Bettiol, Matías Nicolas.
		Mattana, Diego Leonel.
*/

#include <stdio.h>
#include <stdlib.h>
#include "../includes/Assembler.h"
#include "../includes/Cod3D.h"

extern l_if, l_while, cantLabels; // Hace falta para saber siempre que cantidad de labels de cada tipo hay.
extern cantOffset;
int labelFunc = -1;
int auxiliar;
int offsetAux = 0;

// Cabecera del archivo que contiene el codigo asembler.
void cabeceraArchivo(char* nombreArchivo, char* nombreFunc)
{
	labelFunc = labelFunc + 1;
	output = fopen(nombreArchivo,"w");
	fprintf(output, "\t.file\t\"%s\"\n",nombreArchivo);
	fprintf(output, "\t.text\n");
	fprintf(output, "\t.globl \t%s\n", nombreFunc);
	fprintf(output, "\t.type \t%s, @function\n", nombreFunc);
	fprintf(output, "%s:\n", nombreFunc);
	fprintf(output, ".LFB%i:\n", labelFunc);
	fprintf(output, "\t%-8s %s\n","pushl","%ebp");
	fprintf(output, "\t%-8s %s%s\n","movl","%esp",", %ebp");
}

// Fin del archivo que contiene el codigo asembler.
void pieArchivo(char* nombreFunc)
{
	labelFunc = labelFunc;
	fprintf(output, "\n\t%-8s\n","leave");
	fprintf(output, "\tret\n");
	fprintf(output, ".LFE%i:\n", labelFunc);
	fprintf(output, "\t%-8s %s, .-%s\n", ".size", nombreFunc, nombreFunc);
	fprintf(output,"\t.ident  \"fdb--: GCC: (Ubuntu/Linario 4.6.1-9 ubuntu3)\"\n");
	fprintf(output,"\t.section	.note.GNU-stack,\"\",@progbits\n");
	fclose(output);
}

// Return.
void retornar(TElemLista *operando){
	if(strcmp(operando->tipo,"int") == 0){
		if ((strcmp(operando->nombre,"Tmp") == 0) || (operando->constante != 0)){
			fprintf(output,"\tmovl\t%i(%%ebp), %%eax\n",operando->offset);
		}
		else{
			fprintf(output,"\tmovl\t$%i, %%eax\n",operando->valorI);
		}
	}
	if(strcmp(operando->tipo,"float") == 0){
		if ((strcmp(operando->nombre,"Tmp") == 0) || (operando->constante != 0)){
			fprintf(output,"\tflds\t%d(%%ebp)\n",operando->offset);
		}
		else{
			fprintf(output,"\tmovl\t$0x%x, %%eax\n",operando->valorI);
			fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",(- cantOffset * 4) - 4);
			fprintf(output,"\tflds\t%d(%%ebp)\n",(- cantOffset * 4) - 4);
		}
	}
}

// Asignacion.
void asignacion(TElemLista *operandoUno, TElemLista *operandoDos){
	if(strcmp(operandoDos->tipo,"int") == 0){
		if (operandoDos->constante == 0){
			fprintf(output,"\tmovl\t$%i, %%eax\n",operandoDos->valorI);
			fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n", operandoUno->offset);
		}
		else{
			fprintf(output,"\tmovl\t%d(%%ebp), %%eax\n",operandoDos->offset);
			fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoUno->offset);
		}
	}
	if(strcmp(operandoDos->tipo,"float") == 0){
		if (operandoDos->constante == 0){
			fprintf(output,"\tmovl\t$0x%x, %%eax\n",operandoDos->valorI);
			fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoUno->offset);
/*			fprintf(output,"\tflds\t%d(%%ebp)\n",(- cantOffset * 4) - 4);*/
		}
		else{
			fprintf(output,"\tmovl\t%d(%%ebp), %%eax\n",operandoDos->offset);
			fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoUno->offset);
		}
	}
}

// Suma de enteros.
void sumaI(TElemLista *operandoUno, TElemLista *operandoDos, TElemLista *operandoTres){
	if ((operandoUno->constante == 0) && (operandoDos->constante == 0)){
			fprintf(output,"\tmovl\t$%i, %%eax\n",operandoUno->valorI + operandoDos->valorI);
			fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
	}
	else{
		if ((operandoUno->constante == 0) && (operandoDos->constante != 0)){
			fprintf(output,"\tmovl\t%d(%%ebp), %%eax\n",operandoDos->offset);
			fprintf(output,"\taddl\t$%i, %%eax\n",operandoUno->valorI);
			fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
		}
		else{
			if ((operandoUno->constante != 0) && (operandoDos->constante == 0)){
				fprintf(output,"\tmovl\t%d(%%ebp), %%eax\n",operandoUno->offset);
				fprintf(output,"\taddl\t$%i, %%eax\n",operandoDos->valorI);
				fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
			}
			else{
				fprintf(output,"\tmovl\t%d(%%ebp), %%eax\n",operandoUno->offset);
				fprintf(output,"\tmovl\t%d(%%ebp), %%edx\n",operandoDos->offset);
				fprintf(output,"\taddl\t%%edx, %%eax\n");
				fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
			}
		}
	}
}

//Suma de float
void sumaF(TElemLista *operandoUno, TElemLista *operandoDos, TElemLista *operandoTres){
	if ((operandoUno->constante != 0) && (operandoDos->constante == 0)){
		fprintf(output,"\tflds\t%d(%%ebp)\n",operandoUno->offset);
		fprintf(output,"\tmovl\t$0x%x, %%eax\t\n",operandoDos->valorI);
		fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",(- cantOffset * 4) - 4);
		fprintf(output,"\tflds\t%d(%%ebp)\n",(- cantOffset * 4) - 4);
		fprintf(output,"\tfaddp\t%%st, %%st(1)\n");
		fprintf(output,"\tfstps\t%d(%%ebp)\n",operandoTres->offset);
		fprintf(output,"\tflds\t%d(%%ebp)\n",operandoTres->offset);
	}
	else{
		if ((operandoUno->constante == 0) && (operandoDos->constante == 0)){
			fprintf(output,"\tmovl\t$0x%x, %%eax\n",operandoTres->valorI);
			fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
			fprintf(output,"\tflds\t%d(%%ebp)\n",operandoTres->offset);
		}	
		else{
			if((operandoUno->constante == 0) && (operandoDos->constante != 0)){
		   		fprintf(output,"\tflds\t%d(%%ebp)\n",operandoDos->offset);
				fprintf(output,"\tmovl\t$0x%x, %%eax\t\n",operandoUno->valorI);
				fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",(- cantOffset * 4) - 4);
				fprintf(output,"\tflds\t%d(%%ebp)\n",(- cantOffset * 4) - 4);
				fprintf(output,"\tfaddp\t%%st, %%st(1)\n");
				fprintf(output,"\tfstps\t%d(%%ebp)\n",operandoTres->offset);
				fprintf(output,"\tflds\t%d(%%ebp)\n",operandoTres->offset);
			}
			else{
				fprintf(output,"\tflds\t%d(%%ebp)\n",operandoUno->offset); 
				fprintf(output,"\tflds\t%d(%%ebp)\n",operandoDos->offset);
				fprintf(output,"\tfaddp\t%%st, %%st(1)\n");
				fprintf(output,"\tfstps\t%d(%%ebp)\n",operandoTres->offset);
				fprintf(output,"\tflds\t%d(%%ebp)\n",operandoTres->offset);
			}
		}
	}
}

// Resta de enteros.
void restaI(TElemLista *operandoUno, TElemLista *operandoDos, TElemLista *operandoTres){
	if ((operandoUno->constante == 0) && (operandoDos->constante == 0)){
			fprintf(output,"\tmovl\t$%i, %%eax\n",operandoUno->valorI - operandoDos->valorI);
			fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
	}
	else{
		if ((operandoUno->constante == 0) && (operandoDos->constante != 0)){
			/* Forma nuestra. */
			//fprintf(output,"\tmovl\t%d(%%ebp), %%eax\n",operandoDos->offset);
			//fprintf(output,"\tsubl\t%%eax, $%i\n",operandoUno->valorI);
			/* Forma asembler GNU. */
			fprintf(output,"\tmovl\t$%i, %%eax\n",operandoUno->valorI);
			fprintf(output,"\tsubl\t%d(%%ebp), %%eax\n",operandoDos->offset);		
			fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);	
		}
		else{
			if ((operandoUno->constante != 0) && (operandoDos->constante == 0)){
				fprintf(output,"\tmovl\t%d(%%ebp), %%eax\n",operandoUno->offset);
				fprintf(output,"\tsubl\t$%i, %%eax\n",operandoDos->valorI);
				fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
			}
			else{
				/* Forma nuestra. */
				//fprintf(output,"\tmovl\t%d(%%ebp), %%eax\n",operandoUno->offset);
				// fprintf(output,"\tmovl\t%d(%%ebp), %%edx\n",operandoDos->offset);
				// fprintf(output,"\tsubl\t%%eax, %%edx\n");
				// fprintf(output,"\tmovl\t%%edx, %%eax\n");
				/* Forma asembler GNU. */
				fprintf(output,"\tmovl\t%d(%%ebp), %%eax\n",operandoDos->offset);
				fprintf(output,"\tmovl\t%d(%%ebp), %%edx\n",operandoUno->offset);
				fprintf(output,"\tmovl\t%%edx, %%ecx\n");
				fprintf(output,"\tsubl\t%%eax, %%ecx\n");
				fprintf(output,"\tmovl\t%%ecx, %%eax\n");
				fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
			}
		}
	}
}

// Resta de float
void restaF(TElemLista *operandoUno, TElemLista *operandoDos, TElemLista *operandoTres){
	if ((operandoUno->constante != 0) && (operandoDos->constante == 0)){
		fprintf(output,"\tflds\t%d(%%ebp)\n",operandoUno->offset);
		fprintf(output,"\tmovl\t$0x%x, %%eax\n",operandoDos->valorI);
		fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",(- cantOffset * 4) - 4);
		fprintf(output,"\tflds\t%d(%%ebp)\n",(- cantOffset * 4) - 4);
		fprintf(output,"\tfsubrp\t %%st, %%st(1)\n");
		fprintf(output,"\tfstps\t%d(%%ebp)\n",operandoUno->offset);
		fprintf(output,"\tmovl\t%d(%%ebp), %%eax\n",operandoUno->offset);
		fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
		fprintf(output,"\tflds\t%d(%%ebp)\n",operandoTres->offset);
	}
	else{
		if ((operandoUno->constante ==0) && (operandoDos->constante == 0)){
			fprintf(output,"\tmovl\t$0x%x, %%eax\n",operandoTres->valorI);
			fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
			fprintf(output,"\tflds\t%d(%%ebp)\n",operandoTres->offset);
			}	
		else{
			if((operandoUno->constante ==0) && (operandoDos->constante != 0)){
		   		fprintf(output,"\tflds\t%d(%%ebp)\n",operandoDos->offset);
				fprintf(output,"\tmovl\t$0x%x, %%eax\t\n",operandoUno->valorI);
				fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",(- cantOffset * 4) - 4);
				fprintf(output,"\tflds\t%d(%%ebp)\n",(- cantOffset * 4) - 4);
				fprintf(output,"\tfsubp\t %%st, %%st(1)\n");
				fprintf(output,"\tfstps\t%d(%%ebp)\n",operandoTres->offset);
				fprintf(output,"\tflds\t%d(%%ebp)\n",operandoTres->offset);
			}
			else{
				fprintf(output,"\tflds\t%d(%%ebp)\n",operandoUno->offset);
				fprintf(output,"\tfsubs\t%d(%%ebp)\n",operandoDos->offset);
				fprintf(output,"\tfstps\t%d(%%ebp)\n",operandoTres->offset);
				fprintf(output,"\tflds\t%d(%%ebp)\n",operandoTres->offset);
			}
		}
	}
}

// Multiplicacion de enteros.
void multiplicacionI(TElemLista *operandoUno, TElemLista *operandoDos, TElemLista *operandoTres){
	if ((operandoUno->constante == 0) && (operandoDos->constante == 0)){
			fprintf(output,"\tmovl\t$%i, %%eax\n",operandoUno->valorI * operandoDos->valorI);
			fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
	}
	else{
		if ((operandoUno->constante == 0) && (operandoDos->constante != 0)){
			fprintf(output,"\tmovl\t%d(%%ebp), %%eax\n",operandoDos->offset);
			fprintf(output,"\timull\t$%i, %%eax\n",operandoUno->valorI);
			fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
		}
		else{
			if ((operandoUno->constante != 0) && (operandoDos->constante == 0)){
				fprintf(output,"\tmovl\t%d(%%ebp), %%eax\n",operandoUno->offset);
				fprintf(output,"\timull\t$%i, %%eax\n",operandoDos->valorI);
				fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
			}
			else{
				fprintf(output,"\tmovl\t%d(%%ebp), %%eax\n",operandoUno->offset);
				fprintf(output,"\tmovl\t%d(%%ebp), %%edx\n",operandoDos->offset);
				fprintf(output,"\timull\t%%edx, \t%%eax\n");
				fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
			}
		}
	}
}

// Multiplicacion de float.
void multiplicacionF(TElemLista *operandoUno, TElemLista *operandoDos, TElemLista *operandoTres){
	if ((operandoUno->constante != 0) && (operandoDos->constante == 0)){
		fprintf(output,"\tflds\t%d(%%ebp)\n",operandoUno->offset);
		fprintf(output,"\tmovl\t$0x%x, %%eax\n",operandoDos->valorI);
		fprintf(output,"\tmovl\t%%eax\t,%d(%%ebp)\n",(- cantOffset * 4) - 4);
		fprintf(output,"\tflds\t%d(%%ebp)\n",(- cantOffset * 4) - 4);
		fprintf(output,"\tfmulp\t%%st, %%st(1)\n");
		fprintf(output,"\tfstps\t%d(%%ebp)\n",operandoTres->offset);
		fprintf(output,"\tflds\t%d(%%ebp)\n",operandoTres->offset);
	}
	else{
		if ((operandoUno->constante ==0) && (operandoDos->constante == 0)){
			fprintf(output,"\tmovl\t$0x%x, %%eax\n",operandoTres->valorI);
			fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
			fprintf(output,"\tflds\t%d(%%ebp)\n",operandoTres->offset);
			}	
		else{
			if((operandoUno->constante ==0) && (operandoDos->constante != 0)){
			   	fprintf(output,"\tflds\t%d(%%ebp)\n",operandoDos->offset);
				fprintf(output,"\tmovl\t$0x%x, %%eax\t\n",operandoUno->valorI);
				fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",(- cantOffset * 4) - 4);
				fprintf(output,"\tflds\t%d(%%ebp)\n",(- cantOffset * 4) - 4);
				fprintf(output,"\tfmulp\t %%st, %%st(1)\n");
				fprintf(output,"\tfstps\t%d(%%ebp)\n",operandoTres->offset);
				fprintf(output,"\tflds\t%d(%%ebp)\n",operandoTres->offset);
			}
			else{
				fprintf(output,"\tflds\t%d(%%ebp)\n",operandoUno->offset); 
				fprintf(output,"\tfmuls\t%d(%%ebp)\n",operandoDos->offset);
				fprintf(output,"\tfstps\t%d(%%ebp)\n",operandoTres->offset);
				fprintf(output,"\tflds\t%d(%%ebp)\n",operandoTres->offset);
			}
		}
	}
}

// Division de enteros.
void divisionI(TElemLista *operandoUno, TElemLista *operandoDos, TElemLista *operandoTres){
	if ((operandoUno->constante == 0) && (operandoDos->constante == 0)){
			fprintf(output,"\tmovl\t$%i, %%eax\n",operandoUno->valorI / operandoDos->valorI);
			fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
	}
	else{
		if ((operandoUno->constante == 0) && (operandoDos->constante != 0)){
			fprintf(output,"\tmovl\t$%i, %%eax\n",operandoUno->valorI);
			fprintf(output,"\tcltd\n");
			fprintf(output,"\tidivl\t%d(%%ebp)\n",operandoDos->offset);
			fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
		}
		else{
			if ((operandoUno->constante != 0) && (operandoDos->constante == 0)){
				fprintf(output,"\tmovl\t%d(%%ebp), %%eax\n",operandoUno->offset);
				fprintf(output,"\tcltd\n");
				fprintf(output,"\tmovl\t$%i, %d(%%ebp)\n",operandoDos->valorI,operandoTres->offset);
				fprintf(output,"\tidivl\t%d(%%ebp)\n",operandoTres->offset);
				fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
			}
			else{
				fprintf(output,"\tmovl\t%d(%%ebp), %%eax\n",operandoUno->offset);
				fprintf(output,"\tcltd\n");
				fprintf(output,"\tidivl\t%d(%%ebp)\n",operandoDos->offset);
				fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
			}
		}
	}
}

// Division de float.
void divisionF(TElemLista *operandoUno, TElemLista *operandoDos, TElemLista *operandoTres){
	if ((operandoUno->constante != 0) && (operandoDos->constante == 0)){
		fprintf(output,"\tflds\t%d(%%ebp)\n",operandoUno->offset);
		fprintf(output,"\tmovl\t$0x%x,\t%%eax\t\n",operandoDos->valorI);
		fprintf(output,"\tmovl\t%%eax\t,\t%d(%%ebp)\n",(- cantOffset * 4) - 4);
		fprintf(output,"\tflds\t%d(%%ebp)\n",(- cantOffset * 4) - 4);
		fprintf(output,"\tfdivrp\t %%st , %%st(1)\n");
		fprintf(output,"\tfstps\t%d(%%ebp)\n",operandoTres->offset);
		fprintf(output,"\tflds\t%d(%%ebp)\n",operandoTres->offset);
	}
	else{
		if ((operandoUno->constante ==0) && (operandoDos->constante == 0)){
			fprintf(output,"\tmovl\t$0x%x\t,\t%%eax\n",operandoTres->valorI);
			fprintf(output,"\tmovl\t%%eax\t,\t%d(%%ebp)\n",operandoTres->offset);
			fprintf(output,"\tflds\t%d(%%ebp)\n",operandoTres->offset);
		}	
		else{
			if((operandoUno->constante ==0) && (operandoDos->constante != 0)){
				fprintf(output,"\tflds\t%d(%%ebp)\n",operandoDos->offset);
				fprintf(output,"\tmovl\t$0x%x,\t%%eax\t\n",operandoUno->valorI);
				fprintf(output,"\tmovl\t%%eax\t,\t%d(%%ebp)\n",(- cantOffset * 4) - 4);
				fprintf(output,"\tflds\t%d(%%ebp)\n",(- cantOffset * 4) - 4);
				fprintf(output,"\tfdivrp\t %%st , %%st(1)\n");
				fprintf(output,"\tfstps\t%d(%%ebp)\n",operandoTres->offset);
				fprintf(output,"\tflds\t%d(%%ebp)\n",operandoTres->offset);
			}
			else{
				fprintf(output,"\tflds\t%d(%%ebp)\n",operandoUno->offset); 
				fprintf(output,"\tfsdvirp\t%d(%%ebp)\n",operandoDos->offset);
				fprintf(output,"\tfstps\t%d(%%ebp)\n",operandoTres->offset);
				fprintf(output,"\tflds\t%d(%%ebp)\n",operandoTres->offset);
			}
		}
	}
}

// Resto de la division de enteros.
void restoI(TElemLista *operandoUno, TElemLista *operandoDos, TElemLista *operandoTres){
	if ((operandoUno->constante == 0) && (operandoDos->constante == 0)){
			fprintf(output,"\tmovl\t$%i, %%eax\n",operandoUno->valorI % operandoDos->valorI);
			fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
	}
	else{
		if ((operandoUno->constante == 0) && (operandoDos->constante != 0)){
			fprintf(output,"\tmovl\t$%i, %%eax\n",operandoUno->valorI);
			fprintf(output,"\tmovl\t%%eax, %%edx\n");
			fprintf(output,"\tsarl\t$31, %%edx\n");
			fprintf(output,"\tmovl\t%d(%%ebp), %%ebx\n",operandoDos->offset);
			fprintf(output,"\tidivl\t%%ebx\n");
			fprintf(output,"\tmovl\t%%edx, %d(%%ebp)\n", operandoTres->offset);
	
		}
		else{
			if ((operandoUno->constante != 0) && (operandoDos->constante == 0)){
				fprintf(output,"\tmovl\t%d(%%ebp), %%eax\n",operandoUno->offset);
				fprintf(output,"\tmovl\t$%i, %%ebx\n",operandoDos->valorI);
		    	fprintf(output,"\tcltd\n");
				fprintf(output,"\tidivl\t%%ebx\n");
				fprintf(output,"\tmovl\t%%edx, %d(%%ebp)\n",operandoTres->offset);

			}
			else{
				fprintf(output,"\tmovl\t%d(%%ebp), %%eax\n",operandoUno->offset);
				fprintf(output,"\tmovl\t%%eax, %%edx\n");
				fprintf(output,"\tsarl\t$31, %%edx\n");
				fprintf(output,"\tidivl\t%d(%%ebp)\n",operandoDos->offset);
				fprintf(output,"\tmovl\t%%edx, %d(%%ebp)\n", operandoTres->offset);
			}
		}
	}
}

// O Logico (||) sobre enteros..
void orLogicoI(TElemLista *operandoUno, TElemLista *operandoDos, TElemLista *operandoTres){
	if ((operandoUno->constante == 0) && (operandoDos->constante == 0)){
		fprintf(output,"\tmovl\t$%i, %%eax\n",operandoUno->valorI || operandoDos->valorI);
		fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
	}
	else{
		if ((operandoUno->constante == 0) && (operandoDos->constante != 0)){
			fprintf(output,"\tmovl\t$%i, %d(%%ebp)\n",operandoUno->valorI || operandoDos->valorI,operandoTres->offset);
		}
		else{
			if ((operandoUno->constante != 0) && (operandoDos->constante == 0)){
				fprintf(output,"\tmovl\t$%i, %d(%%ebp)\n",operandoUno->valorI || operandoDos->valorI,operandoTres->offset);
			}
			else{
				fprintf(output,"\tcmpl\t$0, %d(%%ebp)\n ",operandoUno->offset);
				fprintf(output,"\tjne\t.L_OR%i\n",cantLabels++);
				fprintf(output,"\tcmpl\t$0, %d(%%ebp)\n ",operandoDos->offset);
				fprintf(output,"\tje\t.L_OR%i\n",cantLabels++);
				fprintf(output,".L_OR%i:\n",cantLabels-2);
				fprintf(output,"\tmovl\t$1, %%eax\n");
				fprintf(output,"\tjmp\t.L_OR%i\n",cantLabels++);
				fprintf(output,".L_OR%i:\n",cantLabels-2);
				fprintf(output,"\tmovl\t$0, %%eax\n");
				fprintf(output,".L_OR%i:\n",cantLabels-1);
				fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
			}
		}
	}
}

// Y Logico (&&) sobre enteros.
void andLogicoI(TElemLista *operandoUno, TElemLista *operandoDos, TElemLista *operandoTres){
	if ((operandoUno->constante == 0) && (operandoDos->constante == 0)){
		fprintf(output,"\tmovl\t$%i, %%eax\n",operandoUno->valorI && operandoDos->valorI);
		fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
	}
	else{
		if ((operandoUno->constante == 0) && (operandoDos->constante != 0)){
			fprintf(output,"\tcmpl\t$0, %d(%%ebp)\n",operandoDos->offset);
			fprintf(output,"\tsetne\t%%al\n");
			fprintf(output,"\tmovzbl\t%%al, %%eax\n");
			fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
		}
		else{
			 if ((operandoUno->constante != 0) && (operandoDos->constante == 0)){
				fprintf(output,"\tcmpl\t$0, %d(%%ebp)\n",operandoUno->offset);
				fprintf(output,"\tsetne\t%%al\n");
				fprintf(output,"\tmovzbl\t%%al, %%eax\n");
				fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
			}
			else{
				fprintf(output,"\tcmpl\t$0, %d(%%ebp)\n ",operandoUno->offset);
				fprintf(output,"\tje\t.L_AND%i\n",cantLabels++);
				fprintf(output,"\tcmpl\t$0, %d(%%ebp) \n",operandoDos->offset);
				fprintf(output,"\tje\t.L_AND%i\n",cantLabels-1);
				fprintf(output,"\tmovl\t$1, %%eax\n");
				fprintf(output,"\tjmp\t.L_AND%i\n",cantLabels++);
				fprintf(output,".L_AND%i:\n",cantLabels-2);
				fprintf(output,"\tmovl\t$0, %%eax\n");
				fprintf(output,".L_AND%i:\n",cantLabels-1);
				fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
			}
		}
	}
}

// Comparacion iguales sobre enteros.
void cmpIgualesI(TElemLista *operandoUno, TElemLista *operandoDos, TElemLista *operandoTres){
	if ((operandoUno->constante == 0) && (operandoDos->constante == 0)){
		fprintf(output,"\tmovl\t$%i, %%eax\n",operandoUno->valorI == operandoDos->valorI);
		fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
	}
	else{
		if ((operandoUno->constante == 0) && (operandoDos->constante != 0)){
			fprintf(output,"\tcmpl\t$%i, %d(%%ebp)\n",operandoUno->valorI,operandoDos->offset);
			fprintf(output,"\tsete\t%%al\n");
			fprintf(output,"\tmovzbl\t%%al, %%eax\n");
			fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
		}
		else{
			 if ((operandoUno->constante != 0) && (operandoDos->constante == 0)){
				fprintf(output,"\tcmpl\t$%i, %d(%%ebp)\n",operandoDos->valorI,operandoUno->offset);
				fprintf(output,"\tsete\t%%al\n");
				fprintf(output,"\tmovzbl\t%%al, %%eax\n");
				fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
			}
			else{
				fprintf(output,"\tmovl\t%d(%%ebp), %%eax\n",operandoUno->offset);
				fprintf(output,"\tcmpl\t%d(%%ebp), %%eax\n",operandoDos->offset);
				fprintf(output,"\tsete\t%%al\n");
				fprintf(output,"\tmovzbl\t%%al, %%eax\n");
				fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
			}
		}
	}
}

// Comparacion iguales sobre float.
void cmpIgualesF(TElemLista *operandoUno, TElemLista *operandoDos, TElemLista *operandoTres){
	if ((operandoUno->constante == 0) && (operandoDos->constante == 0)){
		if (operandoUno->valorF == operandoDos->valorF)
			fprintf(output,"\tmovl\t$1, %%eax\t\n");
		else
			fprintf(output,"\tmovl\t$0, %%eax\t\n");
		fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
		fprintf(output,"\tflds\t%d(%%ebp)\n",operandoTres->offset);
	}
	else{
		if ((operandoUno->constante == 0) && (operandoDos->constante != 0)){
			if(operandoUno->valorF == 1.0){
				fprintf(output,"\tflds\t%d(%%ebp)\n",operandoDos->offset);
				fprintf(output,"\tfld1\n");
			}
			else{
				if(operandoUno->valorF == 0.0){
					fprintf(output,"\tflds\t%d(%%ebp)\n",operandoDos->offset);
					fprintf(output,"\tfldz\n");
				}
				else{
					fprintf(output,"\tflds\t%d(%%ebp)\n",operandoDos->offset);
					fprintf(output,"\tmovl\t$0x%x, %%eax\t\n",operandoUno->valorI);
					fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",(- cantOffset * 4) - 4);
					fprintf(output,"\tflds\t%d(%%ebp)\n",(- cantOffset * 4) - 4);
				}
			}
			fprintf(output,"\tfucomip\t%%st(1), %%st\n");
			fprintf(output,"\tfstp\t%%st(0)\n");
			fprintf(output,"\tjne\t.L_EQ%i\n",cantLabels++);
			fprintf(output,"\tmovl\t$0x3f800000, %%eax\n");
			fprintf(output,"\tjmp\t.L_EQ%i\n",cantLabels++);
			fprintf(output,".L_EQ%i:\n",cantLabels-2);
			fprintf(output,"\tmovl\t$0x00000000, %%eax\n");
			fprintf(output,".L_EQ%i:\n",cantLabels-1);
			fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
			fprintf(output,"\tflds\t%d(%%ebp)\n",operandoTres->offset);
		}
		else{
			 if ((operandoUno->constante != 0) && (operandoDos->constante == 0)){
				if(operandoDos->valorF == 1.0){
					fprintf(output,"\tflds\t%d(%%ebp)\n",operandoUno->offset);
					fprintf(output,"\tfld1\n");
				}
				else{
					if(operandoDos->valorF == 0.0){
						fprintf(output,"\tflds\t%d(%%ebp)\n",operandoUno->offset);
						fprintf(output,"\tfldz\n");
					}
					else{
						fprintf(output,"\tflds\t%d(%%ebp)\n",operandoUno->offset);
						fprintf(output,"\tmovl\t$0x%x, %%eax\t\n",operandoDos->valorI);
						fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",(- cantOffset * 4) - 4);
						fprintf(output,"\tflds\t%d(%%ebp)\n",(- cantOffset * 4) - 4);
					}
				}
				fprintf(output,"\tfucomip\t%%st(1), %%st\n");
				fprintf(output,"\tfstp\t%%st(0)\n");
				fprintf(output,"\tjne\t.L_EQ%i\n",cantLabels++);
				fprintf(output,"\tmovl\t$0x3f800000, %%eax\n");
				fprintf(output,"\tjmp\t.L_EQ%i\n",cantLabels++);
				fprintf(output,".L_EQ%i:\n",cantLabels-2);
				fprintf(output,"\tmovl\t$0x00000000, %%eax\n");
				fprintf(output,".L_EQ%i:\n",cantLabels-1);
				fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
				fprintf(output,"\tflds\t%d(%%ebp)\n",operandoTres->offset);
			}
			else{
				fprintf(output,"\tflds\t%d(%%ebp)\n",operandoUno->offset);
				fprintf(output,"\tflds\t%d(%%ebp)\n",operandoDos->offset);
				fprintf(output,"\tfucomip\t%%st(1), %%st\n");
				fprintf(output,"\tfstp\t%%st(0)\n");
				fprintf(output,"\tjne\t.L_EQ%i\n",cantLabels++);
				fprintf(output,"\tmovl\t$0x3f800000, %%eax\n");
				fprintf(output,"\tjmp\t.L_EQ%i\n",cantLabels++);
				fprintf(output,".L_EQ%i:\n",cantLabels-2);
				fprintf(output,"\tmovl\t$0x00000000, %%eax\n");
				fprintf(output,".L_EQ%i:\n",cantLabels-1);
				fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
				fprintf(output,"\tflds\t%d(%%ebp)\n",operandoTres->offset);
			}
		}
	}
}

// Comparacion mayor que sobre enteros.
void cmpMayorI(TElemLista *operandoUno, TElemLista *operandoDos, TElemLista *operandoTres){
	if ((operandoUno->constante == 0) && (operandoDos->constante == 0)){
		fprintf(output,"\tmovl\t$%i, %%eax\n",operandoUno->valorI > operandoDos->valorI);
		fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
	}
	else{
		if ((operandoUno->constante == 0) && (operandoDos->constante != 0)){
			fprintf(output,"\tcmpl\t$%i, %d(%%ebp)\n",operandoUno->valorI - 1,operandoDos->offset);
			fprintf(output,"\tsetle\t%%al\n");
			fprintf(output,"\tmovzbl\t%%al, %%eax\n");
			fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
		}
		else{
			 if ((operandoUno->constante != 0) && (operandoDos->constante == 0)){
				fprintf(output,"\tcmpl\t$%i, %d(%%ebp)\n",operandoDos->valorI,operandoUno->offset);
				fprintf(output,"\tsetg\t%%al\n");
				fprintf(output,"\tmovzbl\t%%al, %%eax\n");
				fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
			}
			else{
				fprintf(output,"\tmovl\t%d(%%ebp), %%eax\n",operandoUno->offset);
				fprintf(output,"\tcmpl\t%d(%%ebp), %%eax\n",operandoDos->offset);
				fprintf(output,"\tsetg\t%%al\n");
				fprintf(output,"\tmovzbl\t%%al, %%eax\n");
				fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
			}
		}
	}
}

// Comparacion mayor que sobre float.
void cmpMayorF(TElemLista *operandoUno, TElemLista *operandoDos, TElemLista *operandoTres){
	if ((operandoUno->constante == 0) && (operandoDos->constante == 0)){
		if (operandoUno->valorF > operandoDos->valorF)
			fprintf(output,"\tmovl\t$1, %%eax\t\n");
		else
			fprintf(output,"\tmovl\t$0, %%eax\t\n");
		fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
		fprintf(output,"\tflds\t%d(%%ebp)\n",operandoTres->offset);
	}
	else{
		if ((operandoUno->constante == 0) && (operandoDos->constante != 0)){
			if(operandoUno->valorF == 1.0){
				fprintf(output,"\tfld1\n");
				fprintf(output,"\tflds\t%d(%%ebp)\n",operandoDos->offset);
			}
			else{
				if(operandoUno->valorF == 0.0){
					fprintf(output,"\tfldz\n");
					fprintf(output,"\tflds\t%d(%%ebp)\n",operandoDos->offset);
				}
				else{
					fprintf(output,"\tflds\t%d(%%ebp)\n",(- cantOffset * 4) - 4);
					fprintf(output,"\tmovl\t$0x%x, %%eax\t\n",operandoUno->valorI);
					fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",(- cantOffset * 4) - 4);
					fprintf(output,"\tflds\t%d(%%ebp)\n",operandoDos->offset);
				}
			}
			fprintf(output,"\tfxch\t%%st(1)\n");
			fprintf(output,"\tfucomip\t%%st(1), %%st\n");
			fprintf(output,"\tfstp\t%%st(0)\n");
			fprintf(output,"\tseta\t%%al\n");
			fprintf(output,"\ttestb\t%%al, %%al\n");
			fprintf(output,"\tje\t.L_MAYOR%i\n",cantLabels++);
			fprintf(output,"\tmovl\t$0x3f800000, %%eax\n");
			fprintf(output,"\tjmp\t.L_MAYOR%i\n",cantLabels++);
			fprintf(output,".L_MAYOR%i:\n",cantLabels-2);
			fprintf(output,"\tmovl\t$0x00000000, %%eax\n");
			fprintf(output,".L_MAYOR%i:\n",cantLabels-1);
			fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
			fprintf(output,"\tflds\t%d(%%ebp)\n",operandoTres->offset);
		}
		else{
			 if ((operandoUno->constante != 0) && (operandoDos->constante == 0)){
				if(operandoDos->valorF == 1.0){
					fprintf(output,"\tflds\t%d(%%ebp)\n",operandoUno->offset);
					fprintf(output,"\tfld1\n");
				}
				else{
					if(operandoDos->valorF == 0.0){
						fprintf(output,"\tflds\t%d(%%ebp)\n",operandoUno->offset);
						fprintf(output,"\tfldz\n");
					}
					else{
						fprintf(output,"\tflds\t%d(%%ebp)\n",operandoUno->offset);
						fprintf(output,"\tmovl\t$0x%x, %%eax\t\n",operandoDos->valorI);
						fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",(- cantOffset * 4) - 4);
						fprintf(output,"\tflds\t%d(%%ebp)\n",(- cantOffset * 4) - 4);
					}
				}
				fprintf(output,"\tfxch\t%%st(1)\n");
				fprintf(output,"\tfucomip\t%%st(1), %%st\n");
				fprintf(output,"\tfstp\t%%st(0)\n");
				fprintf(output,"\tseta\t%%al\n");
				fprintf(output,"\ttestb\t%%al, %%al\n");
				fprintf(output,"\tje\t.L_MAYOR%i\n",cantLabels++);
				fprintf(output,"\tmovl\t$0x3f800000, %%eax\n");
				fprintf(output,"\tjmp\t.L_MAYOR%i\n",cantLabels++);
				fprintf(output,".L_MAYOR%i:\n",cantLabels-2);
				fprintf(output,"\tmovl\t$0x00000000, %%eax\n");
				fprintf(output,".L_MAYOR%i:\n",cantLabels-1);
				fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
				fprintf(output,"\tflds\t%d(%%ebp)\n",operandoTres->offset);
			}
			else{
				fprintf(output,"\tflds\t%d(%%ebp)\n",operandoUno->offset);
				fprintf(output,"\tflds\t%d(%%ebp)\n",operandoDos->offset);
				fprintf(output,"\tfxch\t%%st(1)\n");
				fprintf(output,"\tfucomip\t%%st(1), %%st\n");
				fprintf(output,"\tfstp\t%%st(0)\n");
				fprintf(output,"\tseta\t%%al\n");
				fprintf(output,"\ttestb\t%%al, %%al\n");
				fprintf(output,"\tje\t.L_MAYOR%i\n",cantLabels++);
				fprintf(output,"\tmovl\t$0x3f800000, %%eax\n");
				fprintf(output,"\tjmp\t.L_MAYOR%i\n",cantLabels++);
				fprintf(output,".L_MAYOR%i:\n",cantLabels-2);
				fprintf(output,"\tmovl\t$0x00000000, %%eax\n");
				fprintf(output,".L_MAYOR%i:\n",cantLabels-1);
				fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
				fprintf(output,"\tflds\t%d(%%ebp)\n",operandoTres->offset);
			}
		}
	}
}

// Comparacion menor que sobre int.
void cmpMenorI(TElemLista *operandoUno, TElemLista *operandoDos, TElemLista *operandoTres){
	if ((operandoUno->constante == 0) && (operandoDos->constante == 0)){
		if (operandoUno->valorI < operandoDos->valorI){
			fprintf(output,"\tmovl\t$1, %%eax\n");
			fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
		}
		else{
			fprintf(output,"\tmovl\t$0, %%eax\n");
			fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
		}
	}
	else{
		if ((operandoUno->constante == 0) && (operandoDos->constante != 0)){
			fprintf(output,"\tcmpl\t$%i, %d(%%ebp)\n",operandoUno->valorI,operandoTres->offset);
			fprintf(output,"\tsetl\t%%al\n");
			fprintf(output,"\tmovzbl\t%%al, %%eax\n");
			fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
		}
		else{
			if ((operandoUno->constante != 0) && (operandoDos->constante == 0)){
				if (operandoDos->valorI > 0){
					fprintf(output,"\tcmpl\t$%i, %d(%%ebp)\n",operandoUno->valorI - 1,operandoTres->offset);
					fprintf(output,"\tsetle\t%%al\n");
					fprintf(output,"\tmovzbl\t%%al, %%eax\n");
					fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
				}
				if (operandoDos->valorI == 0){
					fprintf(output,"\tmovl\t%d(%%ebp), %%eax\n",operandoTres->offset);
					fprintf(output,"\tshrl\t$31, %%eax\n");
					fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
				}
			}
			else{
				fprintf(output,"\tmovl\t%d(%%ebp), %%eax\n",operandoUno->offset);
				fprintf(output,"\tcmpl\t%d(%%ebp), %%eax\n",operandoDos->offset);
				fprintf(output,"\tsetl\t%%al\n");
				fprintf(output,"\tmovzbl\t%%al, %%eax\n");
				fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
			}
		}
	}
}

// Comparacion menor que sobre float.
void cmpMenorF(TElemLista *operandoUno, TElemLista *operandoDos, TElemLista *operandoTres){
	if ((operandoUno->constante == 0) && (operandoDos->constante == 0)){
		if (operandoUno->valorF < operandoDos->valorF)
			fprintf(output,"\tmovl\t$0x3f800000, %%eax\t\n");
		else
			fprintf(output,"\tmovl\t$0x00000000, %%eax\t\n");
		fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
		fprintf(output,"\tflds\t%d(%%ebp)\n",operandoTres->offset);
	}
	else{
		if ((operandoUno->constante == 0) && (operandoDos->constante != 0)){
			if(operandoUno->valorF == 1.0){
				fprintf(output,"\tflds\t%d(%%ebp)\n",operandoDos->offset);
				fprintf(output,"\tfld1\n");
			}
			else{
				if(operandoUno->valorF == 0.0){
					fprintf(output,"\tflds\t%d(%%ebp)\n",operandoDos->offset);
					fprintf(output,"\tfldz\n");
				}
				else{
					fprintf(output,"\tflds\t%d(%%ebp)\n",operandoDos->offset);
					fprintf(output,"\tmovl\t$0x%x, %%eax\t\n",operandoUno->valorI);
					fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",(- cantOffset * 4) - 4);
					fprintf(output,"\tflds\t%d(%%ebp)\n",(- cantOffset * 4) - 4);
				}
			}
			fprintf(output,"\tfxch\t%%st(1)\n");
			fprintf(output,"\tfucomip\t%%st(1), %%st\n");
			fprintf(output,"\tfstp\t%%st(0)\n");
			fprintf(output,"\tseta\t%%al\n");
			fprintf(output,"\ttestb\t%%al, %%al\n");
			fprintf(output,"\tje\t.L_MENOR%i\n",cantLabels++);
			fprintf(output,"\tmovl\t$0x3f800000, %%eax\n");
			fprintf(output,"\tjmp\t.L_MENOR%i\n",cantLabels++);
			fprintf(output,".L_MENOR%i:\n",cantLabels-2);
			fprintf(output,"\tmovl\t$0x00000000, %%eax\n");
			fprintf(output,".L_MENOR%i:\n",cantLabels-1);
			fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
			fprintf(output,"\tflds\t%d(%%ebp)\n",operandoTres->offset);
		}
		else{
			 if ((operandoUno->constante != 0) && (operandoDos->constante == 0)){
				if(operandoDos->valorF == 1.0){
					fprintf(output,"\tfld1\n");
					fprintf(output,"\tflds\t%d(%%ebp)\n",operandoUno->offset);
				}
				else{
					if(operandoDos->valorF == 0.0){
						fprintf(output,"\tfldz\n");
						fprintf(output,"\tflds\t%d(%%ebp)\n",operandoUno->offset);
					}
					else{
						fprintf(output,"\tmovl\t$0x%x, %%eax\t\n",operandoDos->valorI);
						fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",(- cantOffset * 4) - 4);
						fprintf(output,"\tflds\t%d(%%ebp)\n",(- cantOffset * 4) - 4);
					}
				}
				fprintf(output,"\tfxch\t%%st(1)\n");
				fprintf(output,"\tfucomip\t%%st(1), %%st\n");
				fprintf(output,"\tfstp\t%%st(0)\n");
				fprintf(output,"\tseta\t%%al\n");
				fprintf(output,"\ttestb\t%%al, %%al\n");
				fprintf(output,"\tje\t.L_MENOR%i\n",cantLabels++);
				fprintf(output,"\tmovl\t$0x3f800000, %%eax\n");
				fprintf(output,"\tjmp\t.L_MENOR%i\n",cantLabels++);
				fprintf(output,".L_MENOR%i:\n",cantLabels-2);
				fprintf(output,"\tmovl\t$0x00000000, %%eax\n");
				fprintf(output,".L_MENOR%i:\n",cantLabels-1);
				fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
				fprintf(output,"\tflds\t%d(%%ebp)\n",operandoTres->offset);
			}
			else{
				fprintf(output,"\tflds\t%d(%%ebp)\n",operandoDos->offset);
				fprintf(output,"\tflds\t%d(%%ebp)\n",operandoUno->offset);
				fprintf(output,"\tfxch\t%%st(1)\n");
				fprintf(output,"\tfucomip\t%%st(1), %%st\n");
				fprintf(output,"\tfstp\t%%st(0)\n");
				fprintf(output,"\tseta\t%%al\n");
				fprintf(output,"\ttestb\t%%al, %%al\n");
				fprintf(output,"\tje\t.L_MENOR%i\n",cantLabels++);
				fprintf(output,"\tmovl\t$0x3f800000, %%eax\n");
				fprintf(output,"\tjmp\t.L_MENOR%i\n",cantLabels++);
				fprintf(output,".L_MENOR%i:\n",cantLabels-2);
				fprintf(output,"\tmovl\t$0x00000000, %%eax\n");
				fprintf(output,".L_MENOR%i:\n",cantLabels-1);
				fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoTres->offset);
				fprintf(output,"\tflds\t%d(%%ebp)\n",operandoTres->offset);
			}
		}
	}
}

// Complemento de enteros.
void complementoI(TElemLista *operandoUno, TElemLista *operandoDos){
	if (operandoUno->constante == 0){
		if (operandoDos->valorI == 0){
			fprintf(output,"\tmovl\t$1, %%eax\n");
			fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoDos->offset);
		}
		else{
			fprintf(output,"\tmovl\t$0, %%eax\n");
			fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoDos->offset);
		}
	}
	else{
		fprintf(output,"\tcmpl\t$0, %d(%%ebp)\n",operandoUno->offset);
		fprintf(output,"\tsete\t%%al\n");
		fprintf(output,"\tmovzbl\t%%al, %%eax\n");
		fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoDos->offset);
	}
}

// Negacion de float
void negacionF(TElemLista *operandoUno, TElemLista *operandoDos){
	if (operandoUno->constante == 0){
		fprintf(output,"\tmovl\t$0x%x, %%eax\n",operandoUno->valorI);
		fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoDos->offset);
		fprintf(output,"\tflds\t%d(%%ebp)\n",operandoDos->offset);
		fprintf(output,"\tfchs\n");
		fprintf(output,"\tfstps\t%d(%%ebp)\n",operandoDos->offset);
	}
	else{
		fprintf(output,"\tflds\t%d(%%ebp)\n",operandoUno->offset);
		fprintf(output,"\tfchs\n");
		fprintf(output,"\tfstps\t%d(%%ebp)\n",operandoDos->offset);
	}
}

// Negacion de enteros.
void negacionI(TElemLista *operandoUno, TElemLista *operandoDos){
	if (operandoUno->constante == 0){
		fprintf(output,"\tmovl\t$%i, %%eax\n",operandoUno->valorI); 
		fprintf(output,"\tnegl\t%%eax\n");
		fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoDos->offset);
	}
	else{
		fprintf(output,"\tmovl\t%d(%%ebp), %%eax\n",operandoUno->offset);
		fprintf(output,"\tnegl\t%%eax\n");
		fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n",operandoDos->offset);
	}
}

// Casteo de entero a float.
void cast(TElemLista *operandoUno, TElemLista *operandoTres){
	if(operandoUno->constante == 0){
		fprintf(output,"\tmovl\t$%i, %%eax\n",operandoUno->valorI);
		fprintf(output,"\tmovl\t%%eax, %d(%%ebp)\n", (- cantOffset * 4) - 4);
		fprintf(output,"\tfildl\t%d(%%ebp)\n",(- cantOffset * 4) - 4);
		fprintf(output,"\tfstps\t%d(%%ebp)\n",operandoTres->offset);
	}
	else{
		fprintf(output,"\tfildl\t%d(%%ebp)\n",operandoUno->offset);
		fprintf(output,"\tfstps\t%d(%%ebp)\n",operandoTres->offset);
	}
}

// Salto incondicional.
void jump(TElemLista *operando){
	fprintf(output,"\tjmp\t.L%s%i\n",operando->nombre,operando->label);
}

// Salto por negativo para enteros.
void jumpNegativoI(TElemLista *operandoUno, TElemLista *operandoDos){
	fprintf(output,"\tcmpl\t$1, %d(%%ebp)\n",operandoUno->offset);
	fprintf(output,"\tjne\t.L%s%i\n",operandoDos->nombre,operandoDos->label);
}

// Salto por negativo para float.
void jumpNegativoF(TElemLista *operandoUno, TElemLista *operandoDos){
	fprintf(output,"\tcmpl\t$0x3f800000, %d(%%ebp)\n",operandoUno->offset);
	fprintf(output,"\tjne\t.L%s%i\n",operandoDos->nombre,operandoDos->label);
}

// Salto por continue.
void jumpContinue(TElemLista *operandoUno, TElemLista *operandoDos){
/*	fprintf(output,"\tcmpl\t$1, %d(%%ebp)\n",operandoUno->offset);*/
	fprintf(output,"\tjmp\t.L%s%i\n",operandoDos->nombre,operandoDos->label);
}

// Salto por break.
void jumpBreak(TElemLista *operandoUno, TElemLista *operandoDos){
/*	fprintf(output,"\tcmpl\t$1, %d(%%ebp)\n",operandoUno->offset);*/
	fprintf(output,"\tjmp\t.L%s%i\n",operandoDos->nombre,operandoDos->label);
}

// Label.
void label(TElemLista *operando){
	fprintf(output,".L%s%i:\n",operando->nombre,operando->label);
}

// Genera codigo assembler a partir del codigo de tres direcciones.
void generaAssembler(char* nombreArchivo, char* nombreFuncion, Cod3D codigo){
	Cod3D auxiliar = reversa(codigo);
	printListaCodigo(auxiliar);
	TElemCodigo topeCodigo ;
	cabeceraArchivo(nombreArchivo, nombreFuncion);	
	if(cantOffset != 0){
		cantOffset = cantOffset * 4;
		fprintf(output,"\tsubl\t$%i, %%esp\n\n",cantOffset);
	}
	while( auxiliar != NULL){
		topeCodigo = popCodigo(&auxiliar);
		if(strcmp(topeCodigo.enumerado,"RET") == 0){
			retornar(topeCodigo.opDos);
		}

		if(strcmp(topeCodigo.enumerado,"LD") == 0){
			asignacion(topeCodigo.opUno, topeCodigo.opDos);
		}

		if(strcmp(topeCodigo.enumerado,"ADD") == 0){
			if(strcmp(topeCodigo.opUno->tipo,"float") == 0)
				sumaF(topeCodigo.opUno, topeCodigo.opDos, topeCodigo.resultado);
			else
				sumaI(topeCodigo.opUno, topeCodigo.opDos, topeCodigo.resultado);
		}

		if(strcmp(topeCodigo.enumerado,"SUB") == 0){
			if(strcmp(topeCodigo.opUno->tipo,"float") == 0)
				restaF(topeCodigo.opUno, topeCodigo.opDos, topeCodigo.resultado);
			else
				restaI(topeCodigo.opUno, topeCodigo.opDos, topeCodigo.resultado);
		}

		if(strcmp(topeCodigo.enumerado,"MULT") == 0){
			if(strcmp(topeCodigo.opUno->tipo,"float") == 0)
				multiplicacionF(topeCodigo.opUno, topeCodigo.opDos, topeCodigo.resultado);
			else
				multiplicacionI(topeCodigo.opUno, topeCodigo.opDos, topeCodigo.resultado);
		}

		if(strcmp(topeCodigo.enumerado,"DIV") == 0){
			if(strcmp(topeCodigo.opUno->tipo,"float") == 0)
				divisionF(topeCodigo.opUno, topeCodigo.opDos, topeCodigo.resultado);
			else
				divisionI(topeCodigo.opUno, topeCodigo.opDos, topeCodigo.resultado);
		}

		if(strcmp(topeCodigo.enumerado,"MOD") == 0){
			restoI(topeCodigo.opUno, topeCodigo.opDos, topeCodigo.resultado);
		}

		if(strcmp(topeCodigo.enumerado,"OR") == 0){
			orLogicoI(topeCodigo.opUno, topeCodigo.opDos, topeCodigo.resultado);
		}

		if(strcmp(topeCodigo.enumerado,"AND") == 0){
			andLogicoI(topeCodigo.opUno, topeCodigo.opDos, topeCodigo.resultado);
		}

		if(strcmp(topeCodigo.enumerado,"CMPigual") == 0){
			if(strcmp(topeCodigo.opUno->tipo,"float") == 0)
				cmpIgualesF(topeCodigo.opUno, topeCodigo.opDos, topeCodigo.resultado);
			else
				cmpIgualesI(topeCodigo.opUno, topeCodigo.opDos, topeCodigo.resultado);
		}

		if(strcmp(topeCodigo.enumerado,"CMPmayor") == 0){
			if(strcmp(topeCodigo.opUno->tipo,"float") == 0)
				cmpMayorF(topeCodigo.opUno, topeCodigo.opDos, topeCodigo.resultado);
			else
				cmpMayorI(topeCodigo.opUno, topeCodigo.opDos, topeCodigo.resultado);
		}

		if(strcmp(topeCodigo.enumerado,"CMPmenor") == 0){
			if(strcmp(topeCodigo.opUno->tipo,"float") == 0)
				cmpMenorF(topeCodigo.opUno, topeCodigo.opDos, topeCodigo.resultado);
			else
				cmpMenorI(topeCodigo.opUno, topeCodigo.opDos, topeCodigo.resultado);
		}

		if(strcmp(topeCodigo.enumerado,"COMP") == 0){
			complementoI(topeCodigo.opUno, topeCodigo.resultado);
		}

		if(strcmp(topeCodigo.enumerado,"CAST") == 0){
			cast(topeCodigo.opUno, topeCodigo.resultado);
		}

		if(strcmp(topeCodigo.enumerado,"NEG") == 0){
			if(strcmp(topeCodigo.opUno->tipo,"float") == 0)
				negacionF(topeCodigo.opUno, topeCodigo.resultado);
			else
				negacionI(topeCodigo.opUno, topeCodigo.resultado);
		}

		if(strcmp(topeCodigo.enumerado,"JMP") == 0){
			jump(topeCodigo.opUno);
		}

		if(strcmp(topeCodigo.enumerado,"JN") == 0){
			if(strcmp(topeCodigo.opUno->tipo,"float") == 0)
				jumpNegativoF(topeCodigo.opUno, topeCodigo.opDos);
			else
				jumpNegativoI(topeCodigo.opUno, topeCodigo.opDos);
		}

		if(strcmp(topeCodigo.enumerado,"JC") == 0){
			jumpContinue(topeCodigo.opUno, topeCodigo.opDos);
		}

		if(strcmp(topeCodigo.enumerado,"JB") == 0){
			jumpBreak(topeCodigo.opUno, topeCodigo.opDos);
		}

		if(strcmp(topeCodigo.enumerado,"LABEL") == 0){
			label(topeCodigo.opUno);
		}
	}
	pieArchivo(nombreFuncion);
}
